
import requests
import random
import sqlite3
import re
import os
def getpy(bins):
    if(bins==" " or bins=="" or len(bins)!=1):
        return (False,False)
    db=sqlite3.connect(os.path.dirname(__file__)+"/py.db")
    cur=db.cursor()
    '''
    try:
        db.execute("CREATE TABLE 'py' ('zi'  TEXT(2) NOT NULL,'py1'  TEXT,'py2'  TEXT,PRIMARY KEY ('zi'));")
    except:
        print("db has exc")
    else:
        db.commit()
    '''
    cur.execute("select * from py where zi ='%s'"%(bins))
    py=cur.fetchone()
    if(py==None):
        bin=bins.encode("GBK")
        r=requests.post(
            url='http://xh.5156edu.com/index.php',
            headers={
                'Host': 'xh.5156edu.com',
                #'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0',
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                #'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
                #'Accept-Encoding': 'gzip, deflate',
                'Referer': 'http://xh.5156edu.com/',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Upgrade-Insecure-Requests': '1'
            },
            data="f_key={0}&f_type=zi&SearchString.x={1}&SearchString.y={2}".format(('%'+'%'.join( [ "%02X" % x for x in bin ] ).strip()),random.randint(10,20),random.randint(10,20))
        )
        r.encoding="gbk"    
        py1=[]
        py2=[]
        pattern = re.compile(r"class=font_14.*td>")    
        results =  pattern.findall(r.text)   
        #print(len(results))   
        #print (results[0])
        pattern = re.compile(r">.{1,9}<script>")    
        result =  pattern.findall(results[0])   
        for r in result:
            py1.append(r.strip().strip(">").strip(",").rstrip("<script").strip())
        #print(results[0])
        pattern = re.compile(r"\".{1,9}\"")    
        result =  pattern.findall(results[0])  
        #cur.execute() 
        for r in result:
            py2.append(r.strip("\""))
        sql="INSERT INTO 'main'.'py' ('zi', 'py1', 'py2') VALUES ('{0}', '{1}', '{2}')".format(bins,",".join(py1),",".join(py2))
        print(sql)
        db.execute(sql)
        db.commit()
    else:
        py1=py[1].split(",")
        py2=py[2].split(",")
    cur.close()
    db.close()
    return (py1,py2)
def getpys(zi,type=1):
    zi=zi.strip()
    l=len(zi)
    lis=[]
    
    for i in zi:
        t=[]
        (py1,py2)=getpy(i)
        if(type==2):
            for i in py1:
                t.append(i[:1].upper()+i[1:].lower())
            lis.append(t)
        elif(type==3):
            for i in py2:
                t.append(i[0].upper())
            lis.append(t)
        else:
            for i in py2:
                t.append(i[:1].upper()+i[1:].lower())
            lis.append(t)         
    l1=lis[l-1]
    l2=[]
    while l>1:
        l=l-1
        l2=[]
        for i in lis[l-1]:
            for v in l1:
                l2.append(i+v)
        l1=l2
    return set(l1)#列表转集合去重



